package com.jackie.tongzhuang;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.jackie.tongzhuang.Classes.BaseClass;
import com.jackie.tongzhuang.Controls.FenQiList;
import com.jackie.tongzhuang.Controls.TitleBar;
import com.jackie.tongzhuang.DataBase.DbClass;
import com.jackie.tongzhuang.Models.FenQi;

import java.util.ArrayList;
import java.util.List;

public class HuanKuanSelectActivity extends AppCompatActivity {

    private CheckBox qxcheck;
    private TextView hketext,yxhketext,wxhketext;
    private LinearLayout listlay;
    private TitleBar titleBar;

    private DbClass dbClass = new DbClass(this);
    private List<FenQi> fqs;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_huankuan);

        String hke = getIntent().getStringExtra("hke");

        initView();


        //获取本期应还款额
        hketext.setText("¥ "+hke);
        yxhketext.setText("¥ "+hke);
        wxhketext.setText("¥ 0.00");


        //获取本期的还款明细
        fqs = dbClass.getFenQiByData("2025-11-01");

        for(FenQi fq : fqs){
            FenQiList fql = new FenQiList(this,null);
            fql.setId(fq.getId());
            fql.setRiQi(fq.getChangHuanRiQi());
            fql.setJinE(BaseClass.getDecimalToStr(2,fq.getFenQiJinE()));
            fql.setYiChangHuan(fq.getYiChangHuan() == 0 ? false : true);
            fql.setXuanZe(true);
            fql.setBeiZhu(fq.getFenQiRiQi()+" "+fq.getJinHuoDan().getGongHuoShang().getMingCheng()+" 分期 第 "+fq.getQiShu().split("-")[1]+" 期");
            fql.setOnXiangQingCheckedListener(new FenQiList.XiangQingCheckedListener() {
                @Override
                public void onChecked(int id) {
                    double yxhke = 0;
                    for(int i = 0;i<listlay.getChildCount();i++){
                        FenQiList fql = (FenQiList) listlay.getChildAt(i);
                        if(fql.isXuanZe()){
                            yxhke += Double.valueOf(fql.getJinE());
                        }
                    }
                    yxhketext.setText("¥ "+BaseClass.getDecimalToStr(2,yxhke));
                    wxhketext.setText("¥ "+BaseClass.getDecimalToStr(2,(Double.valueOf(hke) - yxhke)));
                }
            });
            listlay.addView(fql);
        }

        qxcheck.setChecked(true);
        qxcheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                for (int i = 0; i < listlay.getChildCount(); i++) {
                    FenQiList fql = (FenQiList) listlay.getChildAt(i);
                    fql.setXuanZe(b);
                }
            }
        });

        titleBar.setText("还款选择");
        titleBar.setOnClickedListener(new TitleBar.TitleBarClickListener() {
            @Override
            public void backClick(View v) {
                finish();
            }

            @Override
            public void submitClick(View v) {
                //遍历listlay下所有的fenqilist，查看是否都选择还款，如果都还款，则直接更新分期已还款和偿还金额
                //如果有未选中的分期，则弹出继续分期页面，让用户继续将这个分期记录再次分期
                ArrayList<Integer> noselect = new ArrayList<>();
                ArrayList<Integer> select = new ArrayList<>();
                for (int i = 0; i < listlay.getChildCount(); i++) {
                    FenQiList fql = (FenQiList) listlay.getChildAt(i);
                    if(!fql.isXuanZe()){
                        //未选中的记录分期Id到noselect数组中
                        noselect.add(fql.getId());
                    }else{
                        //选中的记录分期Id到select数组中
                        select.add(fql.getId());
                    }
                }

                if(select.size()<=0){
                    Toast.makeText(HuanKuanSelectActivity.this,"至少选择一项偿还分期",Toast.LENGTH_SHORT).show();
                    return;
                }
                Intent intent = new Intent(HuanKuanSelectActivity.this, HuanKuanSubmitActivity.class);
                intent.putExtra("noselect",noselect);
                intent.putExtra("select",select);
                startActivity(intent);
                finish();
            }
        });



    }

    private void initView(){
        hketext = findViewById(R.id.hk_main_hke);
        yxhketext = findViewById(R.id.hk_main_yxhke);
        wxhketext = findViewById(R.id.hk_main_wxhke);
        qxcheck = findViewById(R.id.hk_main_qx);
        listlay = findViewById(R.id.hk_main_listlay);
        titleBar = findViewById(R.id.hk_main_titlebar);
    }
}